<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Everyday Series
Description: A three-column, fixed-width blog design.
Version    : 1.0
Released   : 20090617

-->
	<?php
		require_once('functions.php');
		session_start();
		$success = 0;
		$c = 0;
		$d = 0;
		$e = 0;
		?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="template.css" rel="stylesheet" type="text/css" media="screen" />
<title>Registration Form</title>
	<script language="javascript">
			function registerpage(){
				window.location = "register.php";
			}
			
	</script>
	<script src='jquery.js'></script>
	<script>
	$(document).ready(function(){
			  		   
		   $("a").css({
			'text-decoration':'none',
		   });
		   $("a").hover(function () {
				$(this).css({'text-decoration':'none','color':'blue','font-weight':'bold'});
			  }, function () {
				var cssObj = {
				  'text-decoration':'none',
				  'color' : '',
				  'font-weight':''
				}
				$(this).css(cssObj);
			  });
		   
				$("#submit").css({
					'background-color':'white',
					'color':'#2F2825'
				});
				$("#submit").hover(function () {
					$(this).css({'background-color' : '#993333','color':'white','font-weight':'bold'});
				  }, function () {
					var cssObj = {
					  'background-color' : 'white',
					  'color' : '#2F2825',
					  'font-weight':''
					}
					$(this).css(cssObj);
				  });
		   
		 });
		</script>
	</head>
<body>
<!-- start header -->
<div id="header">
	<div id="logo">
		<h1><span>UPLB</span> &nbsp; Leave Application</h1>
		<p></p>
	</div>
</div>
<!-- end header -->
<div id="wrapper">
	<!-- start page -->
	<div id="page">
	<div id="page-bg">
		<div id="sidebar1" class="sidebar">
		
		</div>
		<!-- start content -->
		<div id="content">
			<div class="post">
				<h1 class="title"><strong><a href="#">Registration Form</a></strong></h1>
				<div class="entry">
					<br />
					<form method = "post" action = "register.php">
						<table>
							<tr>
								<td>
								Username: 
								</td>
								<td>
									<input type ="text" name = "username" value="<?php if(isset ($_POST['submit'])) echo $_POST['username']; ?>"/> 									
								</td>
								<td>
									<?php 
									if(isset($_POST['submit'])){
										if($_POST['username'] == ""  ){
											echo "<span class='error'>Enter your username!</span>";$c = 1; 
										} 
										else $c = 0;
									} ?>
								</td>
							</tr>
							<tr>
								<td>
								Password: 
								</td>
								<td>
									<input type ="password" name = "pass1" value="<?php if(isset($_POST['submit'])) echo $_POST['pass1']; ?>"/>
								</td>	
								<td>
									<?php 
										if(isset($_POST['submit'])){
											if($_POST['pass1'] == ""){ 
												echo "<span class='error'>Enter your password!</span>";$d = 1; 
											}
											else $d = 0;} 
									?>
								</td>
							</tr>
							<tr>
								<td>
								Confirm Password: 
								</td>
								<td>
									<input class = "textboxes" type ="password" name = "pass2" value="<?php if(isset($_POST['submit'])) echo $_POST['pass2']; ?>" /> 
								</td>
								<td>
									<?php 
										if(isset($_POST['submit'])){ 
											if($_POST['pass2'] == "" || $_POST['pass1'] != $_POST['pass2']){ 
												echo "<span class='error'>Confirm your password!</span>";$e = 1; 
											}
											else $e = 0;} 
									?>
								</td>
							</tr>
							<tr>
								<td>&nbsp;&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;&nbsp;</td>
								<td>
									<input type = "submit" class = "buttons" value = "Submit" name = "submit" id ="submit">
								</td>
							</tr>
						</table>	
						</form>
						<?php
							if(isset($_POST['submit']) && $c == 0 && $d == 0 && $e == 0){
								
								$link = makeSQLconnection();
								$user = $_POST['username'];
								$password = $_POST['pass1'];
								$q1 = sprintf("SELECT * FROM `USERS` WHERE `Username` = '%s'", $user);
								$r1 = mysql_query($q1, $link);
								if ($r1) {
									if (mysql_num_rows($r1) > 0) {
										echo '<br />This user already registered!';
									} else {
									
										$q2 = sprintf("INSERT INTO `USERS` VALUES ('%s', '%s')", $user, $password);
										$r2 = mysql_query($q2, $link);
										if (!$r2) {
											echo '<br />Query Failed!';
										} else {
											$success = 1;
										}
									}
								} else {
									echo '<br />Unable to execute query:' . mysql_error();
								}
							mysql_close($link);
							}
							if ($success == 1)
								header('Location: index.php');
						
						?>
					<p class="links"><a href="index.php" class="more">&laquo;&laquo;&nbsp;&nbsp;Log In&nbsp;&nbsp;&raquo;&raquo;</a></p>					
				</div>
			</div>
		</div>
		<!-- end content -->
		<!-- start sidebars -->
		<div id="sidebar2" class="sidebar">

		</div>
		<!-- end sidebars -->
		<div style="clear: both;">&nbsp;</div>
	</div>
	</div>
	<!-- end page -->
</div>
<div id="footer">
	
</div>
</body>
</html>
